package com.sams.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sams.entity.School;
import com.sams.entity.Student;
import com.sams.entity.StudentExample;
import com.sams.entity.User;
import com.sams.entity.StudentExample.Criteria;
import com.sams.mapper.SchoolMapper;
import com.sams.mapper.StudentMapper;
import com.sams.service.SchoolService;
import com.sams.service.StudentService;
@Service
public class StudentServiceImpl implements StudentService{

	@Autowired
	private StudentMapper mapper;
	@Autowired
	private SchoolService schoolService;
	
	@Autowired
	private SchoolMapper schoolMapper;
	
	public int add(Student stu) throws Exception {
		//存在学生
		if(isExistStudent(stu.getIdcard(),stu.getSchid(),null)) {
			throw new Exception(stu.getName()+"在当前学期中已存在，不能再次添加");
		}
		return mapper.insert(stu);
	}

	public int delete(int id) {
		return mapper.deleteByPrimaryKey(id);
	}
	
	/**
	 * 模糊查询
	 */
	public PageInfo<Student> listStu(int currentPage,String name,String idcard,Integer schid,User user
			,Integer zid){
		
		  StudentExample example = new StudentExample();
		 Criteria createCriteria = example.createCriteria();
		 //本地用户
		 if(user.getRoleid()==1) { //学校账号
			 createCriteria.andUidEqualTo(user.getId());
		 }else if(user.getRoleid()==2) {//镇级别账号 
			 
			 List<Integer> scIds = new ArrayList<Integer>();
			 List<School> scList = schoolMapper.selectSchoolByUid(user.getId());
			for(School sc : scList) {
				scIds.add(sc.getSchid());
			}
			createCriteria.andSchidIn(scIds);
		 }else {// 3， 4 说明是县级别用户
			 //根据镇id 也是查询所有学校
			 if(zid!=null && !zid.toString().equals("0")) {
				 List<Integer> scIds = new ArrayList<Integer>();
				 List<School> scList = schoolMapper.selectSchoolByZid(zid);
				 for(School sc : scList) {
					 scIds.add(sc.getSchid());
				 }
				 createCriteria.andSchidIn(scIds);
			 }
		 }
		 
		 //按条件查询
		 if(name!=null && !name.equals("")) {
			 createCriteria.andNameEqualTo(name.trim());
		 }
		 if(idcard!=null && !idcard.equals("")) {
			 createCriteria.andIdcardEqualTo(idcard.trim());
		 }
		 if(schid!=null) {
			 createCriteria.andSchidEqualTo(schid);
		 }
			 
		// 1 定义当前页  和 一页显示的行数 
		  PageHelper.startPage(currentPage, 20);  
 		 // 2  调用查询语句
		List<Student> list =   mapper.selectByExample(example);
		
		PageInfo<Student> pageInfo = new PageInfo<Student>(list);
		
		return pageInfo;
	}

	public Student findById(int id) {
		Student stu = mapper.selectByPrimaryKey(id);
		
		return stu;
	}

	public boolean importStudent(String path) {
 		return false;
	}
	
	public int update(Student stu) throws Exception {
		//存在学生
		if(isExistStudent(stu.getIdcard(),stu.getSchid(),stu.getId())) {
			throw new Exception(stu.getName()+"在当前学期中已存在，不能再次添加");
		}
 		return mapper.updateByPrimaryKey(stu);
	}
	/**
	 * 批量导入学生
	 * @throws Exception 
	 */
	public boolean insertBatch(List<Student> stuList) throws Exception {
		 for(Student stu : stuList) {
			School ss =  schoolService.getSchoolByName(stu.getSchoolName(),stu.getXueduan());
			if(ss!=null) {
				 stu.setSchid(ss.getSchid());
			}else {
				 throw new Exception(stu.getName()+"没有对应的学校");
			}
			//存在学生
			if(isExistStudent(stu.getIdcard(),stu.getSchid(),null)) {
				throw new Exception(stu.getName()+"在当前学期中已存在，不能再次添加");
			}
			 mapper.insert(stu);
		 }
		 //若没有异常，则导出成功 
		return true;
	 
	}
	
	/**
	 * 根据身份证、学期id，验证唯一性 
	 * @return
	 */
	public boolean isExistStudent(String idcard,Integer schid,Integer id) {
		 StudentExample example = new StudentExample();
		 Criteria createCriteria = example.createCriteria();
		 createCriteria.andSchidEqualTo(schid);
		 createCriteria.andIdcardEqualTo(idcard);
		 //用于修改时验证，并排除它自己
		 if(id!=null) {
			 createCriteria.andIdNotEqualTo(id);
		 }
		 List<Student> list =mapper.selectByExample(example);
		 if(list!=null && list.size()>0) {
			 return true;  //存在
		 }else {
			 return false;//不存在
		 }
	}

	public void deleteBatch(String ids) {
		String [] array =ids.split(",");
		 mapper.deleteBatch(Arrays.asList(array));
		
	}

 
	
}
